/**
 * Copyright (C) 2012 Eduard Sedov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.es.function.impl;

import java.lang.reflect.Method;
import org.es.function.Function;
import org.es.function.FunctionException;

/**
 * An implementation of {@link Function} interface, that uses reflection to call
 * a method of supplied object.
 * 
 * @author ESEDO
 */
public class FunctionReflection<T> implements Function<T> {

    /** holds the {@link Method} object */
    private Method method;
    /** holds the target object. may be null if {@link #method} is a static method. */
    private Object instance;

    /**
     * Creates a {@linkplain FunctionReflection} object for a static method.
     * @param method the method
     * @throws IllegalArgumentException if the given method is not static
     */
    public FunctionReflection(Method method) throws IllegalArgumentException {
        StaticPredicateMethodFilter.get().accept(method);
        this.method = method;
    }

    /**
     * Creates a {@linkplain FunctionReflection} object for given method and instance.
     * @param method the method 
     * @param instance the target object
     * @throws IllegalArgumentException if the given instance parameter is null and method is not static
     */
    public FunctionReflection(Method method, Object instance) throws IllegalArgumentException {
        if (instance != null) {
            NonStaticPredicateMethodFilter.get().accept(method);
        } else {
            StaticPredicateMethodFilter.get().accept(method);
        }
        this.method = method;
        this.instance = instance;
    }

    /**
     * Getter for {@code method} member.
     * @return method
     */
    public Method getMethod() {
        return method;
    }

    /**
     * Setter for {@code method} member
     * @param method the new value
     */
    public void setMethod(Method method) {
        this.method = method;
    }

    /**
     * Getter for {@code instance} member.
     * @return 
     */
    public Object getInstance() {
        return instance;
    }

    public void setInstance(Object instance) {
        this.instance = instance;
    }

    /** 
     * {@inheritDoc}
     */
    public T invoke(Object... params) {
        if (method == null) {
            throw new IllegalStateException("No method specified.");
        }
        try {
            return (T) method.invoke(instance, params);
        } catch (FunctionException e) {
            throw e;
        } catch (Exception e) {
            throw new FunctionException(e);
        }
    }
}
